#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

ll a, p, b;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

ll bsgs(ll a, ll b, ll c, ll p) {  // a * b ^ x = c (mod p)
  a %= p, b %= p, c %= p;
  if (a == c || p == 1) return 0;
  map<ll, ll> mp;
  ll k = ceil(sqrt(p));
  ll x = 1;
  rep(i, 0, k - 1) {
    mp[x * c % p] = i;
    x = x * b % p;
  }
  b = x, x = 1;
  rep(i, 1, k) {
    x = x * b % p;
    ll y = a * x % p;
    if (mp.count(y)) {
      return i * k - mp[y];
    }
  }
  return -1;
}

ll solve(ll a, ll p, ll b) {
  a %= p, b %= p;
  if (b == 1 || p == 1) return 0;

  ll c = 1, k = 0;
  while (true) {
    ll d = gcd(a, p);
    if (d == 1) break;
    if (b % d != 0) return -1;
    k++;
    p /= d, b /= d;
    c = c * (a / d) % p;
    if (c == b) return k;  // 特殊处理
  }
  ll n = bsgs(c, a, b, p);
  if (n == -1) return -1;
  return n + k;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  while (cin >> a >> p >> b && p) {
    ll ans = solve(a, p, b);
    if (ans == -1)
      cout << "No Solution" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}